package ${packageName}.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import org.monkey.common.cbs.util.PageConvertUtil;
import org.monkey.common.constant.entity.ErrorResultEnum;
import org.monkey.common.constant.exception.BaseException;
import org.monkey.common.constant.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ${entry.entityName?cap_first}ServiceImpl implements ${entry.entityName?cap_first}Service {

  @Autowired
  ${entry.entityName?cap_first}Dao ${entry.entityName?uncap_first}Dao;
  @Autowired
  ${entry.entityName?cap_first}Convert ${entry.entityName?uncap_first}Convert;

  @Override
  public ${entry.entityName?cap_first}DTO getById(Long id, boolean nullThrow) {
    ${entry.entityName?cap_first}PO ${entry.entityName?uncap_first}Po = ${entry.entityName?uncap_first}Dao.selectById(id);
    if (${entry.entityName?uncap_first}Po == null && nullThrow) {
      throw new BaseException(ErrorResultEnum.DATA_NOT_FOUNT);
    }
    return ${entry.entityName?uncap_first}Convert.po2Dto(${entry.entityName?uncap_first}Po);
  }

  @Override
  public ${entry.entityName?cap_first}DTO add(${entry.entityName?cap_first}AddForm addForm) {
    ${entry.entityName?cap_first}DTO ${entry.entityName?uncap_first}Dto = ${entry.entityName?uncap_first}Convert.addForm2Dto(addForm);
    ${entry.entityName?cap_first}PO ${entry.entityName?uncap_first}Po = ${entry.entityName?uncap_first}Convert.dto2Po(${entry.entityName?uncap_first}Dto);
    ${entry.entityName?uncap_first}Dao.insert(${entry.entityName?uncap_first}Po);
    return ${entry.entityName?uncap_first}Convert.po2Dto(${entry.entityName?uncap_first}Po);
  }

  @Override
  public ${entry.entityName?cap_first}DTO update(${entry.entityName?cap_first}UpdateForm updateForm) {
    ${entry.entityName?cap_first}DTO ${entry.entityName?uncap_first}Dto = ${entry.entityName?uncap_first}Convert.updateForm2Dto(updateForm);
    ${entry.entityName?cap_first}PO ${entry.entityName?uncap_first}Po = ${entry.entityName?uncap_first}Convert.dto2Po(${entry.entityName?uncap_first}Dto);
    ${entry.entityName?uncap_first}Dao.updateById(${entry.entityName?uncap_first}Po);
    return ${entry.entityName?uncap_first}Convert.po2Dto(${entry.entityName?uncap_first}Po);
  }

  @Override
  public ${entry.entityName?cap_first}DTO save(${entry.entityName?cap_first}SaveForm saveForm) {
    ${entry.entityName?cap_first}DTO ${entry.entityName?uncap_first}Dto = ${entry.entityName?uncap_first}Convert.saveForm2Dto(saveForm);
    ${entry.entityName?cap_first}PO ${entry.entityName?uncap_first}Po = ${entry.entityName?uncap_first}Convert.dto2Po(${entry.entityName?uncap_first}Dto);
    if (${entry.entityName?uncap_first}Dto.getId() == null) {
      ${entry.entityName?uncap_first}Dao.insert(${entry.entityName?uncap_first}Po);
    } else {
      ${entry.entityName?uncap_first}Dao.updateById(${entry.entityName?uncap_first}Po);
    }
    return ${entry.entityName?uncap_first}Convert.po2Dto(${entry.entityName?uncap_first}Po);
  }

  @Override
  public List<${entry.entityName?cap_first}DTO> listAll() {
    List<${entry.entityName?cap_first}PO> pos = ${entry.entityName?uncap_first}Dao.selectList(new QueryWrapper<>());
    return DataUtil.colTypeChange(pos, po -> ${entry.entityName?uncap_first}Convert.po2Dto(po));
  }

  @Override
  public IPage<${entry.entityName?cap_first}DTO> page(${entry.entityName?cap_first}PageForm pageForm) {
    Page<${entry.entityName?cap_first}PO> page = new Page<>(pageForm.getPageNumber(),
        pageForm.getPageSize());
    IPage<${entry.entityName?cap_first}PO> iPage = ${entry.entityName?uncap_first}Dao
        .page(page, pageForm);
    return PageConvertUtil.convert(iPage, po -> ${entry.entityName?uncap_first}Convert.po2Dto(po));
  }

  @Override
  public List<${entry.entityName?cap_first}DTO> list(${entry.entityName?cap_first}ListForm listForm) {
    List<${entry.entityName?cap_first}PO> pos = ${entry.entityName?uncap_first}Dao.list(listForm);
    return DataUtil.colTypeChange(pos, po -> ${entry.entityName?uncap_first}Convert.po2Dto(po));
  }

  @Override
  public Boolean delete(Long id) {
    ${entry.entityName?uncap_first}Dao.deleteById(id);
    return true;
  }
}
